//
//  GZStatusPictureView.swift
//  sinaweibo
//
//  Created by mac book pro on 16/3/27.
//  Copyright © 2016年 finish. All rights reserved.
//

import UIKit
import SDWebImage
//MARK: -配图相关常量
/// cell 间距
private let GZStatusPictureViewItemMargin: CGFloat = 10
/// 最大列数
private let maxColumn = 3
/// cell的宽高
private let GZStatusPictureViewItemWH: CGFloat = (UIScreen.width - 2 * CZStatusCellMargin - 2 * GZStatusPictureViewItemMargin) / CGFloat(maxColumn)
/// cell的大小
private let GZStatusPictureViewItemSize = CGSize(width: GZStatusPictureViewItemWH, height: GZStatusPictureViewItemWH)

class GZStatusPictureView: UICollectionView {
    //MARK: -属性
    
    
    var pictureURLs: [NSURL]?{
        didSet{
            let viewSize = calsViewSize()
            self.snp_updateConstraints { (make) -> Void in //更新约束 不是创建
                make.size.equalTo(viewSize)
            }
            self.reloadData() //跟新约束 防止服用
        }
    }
    private func calsViewSize() ->CGSize{
        let count = pictureURLs?.count ?? 0
        if count == 0 {
            return CGSizeZero
        }
        if count == 1{
            var size = CGSize(width: 160, height: 120)
            let url = pictureURLs![0]
            // key 就是url路径
            if let image = SDImageCache.sharedImageCache().imageFromDiskCacheForKey(url.absoluteString){
                size  = image.size
            }
            if size.width < 40 {
                size.width = 40
            }else if size.width > UIScreen.width * 0.5{
                size.width = UIScreen.width * 0.5
            }
            layout.itemSize = size
            layout.minimumInteritemSpacing = 0
            layout.minimumLineSpacing = 0
            return size
        }
        layout.itemSize = GZStatusPictureViewItemSize
        layout.minimumInteritemSpacing = GZStatusPictureViewItemMargin
        layout.minimumLineSpacing = GZStatusPictureViewItemMargin
        //列数 默认3
        var column = maxColumn
        if count == 2 || count == 4 {
            column = 2
        }
        //行数
        let row = (count + column - 1) / column
        //计算配图总宽度
        let width = CGFloat(column) * GZStatusPictureViewItemWH + (CGFloat(column) - 1 ) * GZStatusPictureViewItemMargin
        let height = CGFloat(row) * GZStatusPictureViewItemWH + (CGFloat(row) - 1 ) * GZStatusPictureViewItemMargin
        return CGSize(width: width, height: height)
    }
    private let layout = UICollectionViewFlowLayout()
    init(){
        super.init(frame: CGRectZero, collectionViewLayout: layout)
        self.dataSource = self
        self.registerClass(GZStatusPictureViewCell.self, forCellWithReuseIdentifier: "UICollectionViewCell")
        self.backgroundColor = UIColor.clearColor()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
extension GZStatusPictureView : UICollectionViewDataSource{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictureURLs?.count ?? 0
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("UICollectionViewCell", forIndexPath: indexPath) as! GZStatusPictureViewCell
        cell.imageURL = pictureURLs?[indexPath.item]
        return cell
    }
}
///自定义cell
class GZStatusPictureViewCell: UICollectionViewCell{
    
    // MARK: - 属性
    /// 显示图片的URL
    var imageURL: NSURL? {
        didSet {
            if let url = imageURL{
                /// 加载图片
                iconView.sd_setImageWithURL(url)
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func prepareUI(){
        contentView.addSubview(iconView)
        // 添加约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView)
        }
    }
    // MARK: - 懒加载控件
    // 图片
    private lazy var iconView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = UIViewContentMode.ScaleAspectFill
        imageView.clipsToBounds = true
        return imageView
        }()
}
